package io.renren.common.utils;


import com.alibaba.fastjson.JSON;
import io.renren.common.config.BaseConstant;
import io.renren.common.enums.ErrorCode;
import io.renren.common.exception.RRException;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author create by jieshao email:2609207540@qq.com
 * @projectName house-club-api
 * @Description: 阿里实名认证
 * @date 2020-04-14
 */
@Component
public class AliUserVerifyUtil {

    @Resource
    private BaseConstant baseConstant;

    /**
      * @description: 实名认证
      * @auther: create by jieShao email:2609207540@qq.com
      * @date 2020/4/14
      * @param
      * @return
      */
    public Identity verifyUser(String idCard, String name) {
        String path = "/chinadatapay/1882";
        String method = "POST";

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + baseConstant.getAliVerifyUserAppCode());
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        Map<String, String> querys = new HashMap<String, String>();
        querys.put("idcard", idCard);
        querys.put("name", name);
        try {
            HttpResponse response = HttpUtils.doPost(baseConstant.getAliVerifyUserHost(), path, method, headers, querys, (Map<String, String>) null);

            Identity identity = JSON.parseObject(EntityUtils.toString(response.getEntity()), Identity.class);
            //判断状态码
            if (identity.getCode().equalsIgnoreCase("100001")){
                throw new RRException("查询无结果",100001);
            }
            /*if (identity.getCode().equalsIgnoreCase("20010")){
                throw new BusinessException("20010","身份证号为空或非法");
            }
            if (identity.getCode().equalsIgnoreCase("20310")){
                throw new BusinessException("20310","姓名为空或非法");
            }*/
            return identity;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(ErrorCode.ERROR.getMessage(),ErrorCode.ERROR.getCode());
        }
    }

    @Data
    public static class Identity {
        private String code;//返回码，0：成功，非0：失败（详见错误码定义）
        //当code=0时，再判断下面result中的res；当code!=0时，表示调用已失败，无需再继续
        private String message;
//        private ResultBean result;

    }

    @Data
    public static class ResultBean {
        private String name;//姓名
        private String idcard;//身份证号
        private String res;//核验结果状态码，1 一致；2 不一致；3 库无
        /*(1)现役军人、武警官兵、特殊部门人员及特殊级别官员；
        (2)退役不到2年的军人和士兵（根据军衔、兵种不同，时间会有所不同，一般为2年）；
        (3)户口迁出，且没有在新的迁入地迁入；
        (4)户口迁入新迁入地，当地公安系统未将迁移信息上报到公安部（上报时间地域不同而有所差异）；
        (5)更改姓名，当地公安系统未将更改信息上报到公安部（上报时间因地域不同而有所差异）；
        (6)移民；
        (7)未更换二代身份证；
        (8)死亡。
        (9)身份证号确实不存在
        */
        private String description;//核验结果状态描述
        private String sex;
        private String birthday;
        private String address;
    }

    /*public static void main(String[] args) throws IOException {
        String path = "/chinadatapay/1882";
        String method = "POST";

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + "391f538733a448079b04caa295e16db4");
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        Map<String, String> querys = new HashMap<String, String>();
        querys.put("idcard", "511525199204273657");
        querys.put("name", "王杰");
        try {
            HttpResponse response = HttpUtils.doPost("http://checkone.market.alicloudapi.com", path, method, headers, querys, (Map<String, String>) null);

            Identity identity = JSON.parseObject(EntityUtils.toString(response.getEntity()), Identity.class);
            //判断状态码
            if (identity.getCode().equalsIgnoreCase("400")){
                throw new BusinessException("400","参数错误");
            }
            if (identity.getCode().equalsIgnoreCase("20010")){
                throw new BusinessException("20010","身份证号为空或非法");
            }
            if (identity.getCode().equalsIgnoreCase("20310")){
                throw new BusinessException("20310","姓名为空或非法");
            }
            System.out.println("结果为===="+identity.toString());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.ERROR.getCode(),ErrorCode.ERROR.getMessage());
        }
    }*/

}
